#include <base/FdMap.hpp>

FdMap* FdMap::instance_ = nullptr;
std::mutex FdMap::mutex_;

FdMap* FdMap::GetInstance() 
{
    if (instance_ == nullptr) {
        std::lock_guard<std::mutex> lk(mutex_);
        if (instance_ == nullptr) {
            instance_ = new FdMap();
            instance_ -> InitFdMap();
        }
    }
    return instance_;
}

void FdMap::InitFdMap()
{

}

void FdMap::SetFdMap(int fd, const std::string& name) 
{
    std::lock_guard<std::mutex> lk(fd_map_mutex_);
    fdmap_.insert({fd, name});
}

void FdMap::DelFdMap(int fd) 
{
    std::lock_guard<std::mutex> lk(fd_map_mutex_);
    fdmap_.erase(fd);
}

std::string FdMap::FindFdMap(int fd) 
{
    std::lock_guard<std::mutex> lk(fd_map_mutex_);
    auto it = fdmap_.find(fd);
    if (it != fdmap_.end()) {
        return it->second;
    } else {
        return std::string();
    }
}

FdMap::FdMap() 
{

}

FdMap::~FdMap() 
{
    
}